<template>
  <div class="login-page">
    <div class="back">
      <!-- .native绑定组件的原生事件，属于组件根元素 -->
      <!-- $router.back() 返回上一次访问路由，forward go -->
      <geek-icon @click.native="$router.back()" name="esay-close"></geek-icon>
    </div>
    <h3 class="title">短信登录</h3>
    <!-- 表单 -->
    <van-form ref="form">
      <van-field
        name="mobile"
        :rules="rules.mobile"
        placeholder="请输入手机号"
        v-model="form.mobile"
      ></van-field>
      <van-field
        name="code"
        :rules="rules.code"
        placeholder="请输入验证码"
        v-model="form.code"
      >
        <template #button>
          <span class="send" @click="sendCode">{{
            second > 0 ? second : "发送验证码"
          }}</span>
        </template>
      </van-field>
    </van-form>
    <van-button @click="login">登录</van-button>
  </div>
</template>
<script>
export default {
  name: 'LoginPage'
}
</script>
<style scoped lang="less">
.login-page {
  padding: 0 32px;
  .back {
    height: 60px;
    display: flex;
    align-items: center;
    .geek-icon {
      font-size: 20px;
      color: #ccc;
      position: relative;
      left: -15px;
    }
  }
  .title {
    font-size: 22px;
    line-height: 1;
    padding: 30px 0;
  }
  .van-cell {
    padding: 20px 0;
    &::after {
      left: 0;
      right: 0;
    }
  }
  .van-button {
    width: 100%;
    margin-top: 40px;
    height: 50px;
    color: #fff;
    font-size: 16px;
    border: none;
    background: linear-gradient(to right, #ff9999, #ffa179);
  }
}
.send {
  font-size: 12px;
  color: #a5a6ab;
}
</style>
<script>
import { getSMcode, userLogin } from "@/api/user";
import to from "await-to-js";

export default {
  methods: {
    async sendCode() {
      // 防止重复点击发送验证码
      if (this.second > 0) return;

      //这里需要校验手机号
      const [err] = await to(this.$refs.form.validate("mobile"));
      if (err) return this.$toast.fail(err.message);

      // 进行网络请求
      const [err2, data] = await to(getSMcode(this.form.mobile));

      if (err2) return this.$toast.fail(err2);
      // console.log(data)
      //正确之后,使发送验证码按钮变成倒计时
      this.second = 60;
      if (this.timer) clearInterval(this.timer);
      this.timer = setInterval(() => {
        this.second--;
        if (this.second === 0) {
          clearInterval(this.timer);
        }
      }, 1000);
    },
    async login() {
      // 校验手机号和验证码
      const [err] = await to(this.$refs.form.validate());
      // console.log('校验手机号和验证码',err)
      if (err) return this.$toast.fail(err.message);
      //登录请求
      const [err2, data] = await userLogin(this.form);
      if (err2) return this.$toast.fail(err2);
      console.log("我是数据", data);
      //   存储数据
      this.$store.commit("user/setToken", data.data.data.token);
          this.$store.commit('user/setRefreshToken', data.data.data.refresh_token)
      //跳转页面
      this.$router.replace(this.$route.query.returnUrl);
    },
  },
  beforeDestroy() {
    if (this.timer) clearInterval(this.timer);
  },
  data() {
    return {
      timer: "",
      second: 0,
      form: {
        mobile: "",
        code: "",
      },
      rules: {
        mobile: [
          { required: true, message: "请输入正确手机号" },
          {
            pattern:
              /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
            message: "请输入正确手机号",
          },
        ],
        code: [
          { required: true, message: "请输入正确密码" },
          {
            pattern: /^\d{6}$/,
            message: "请输入正确验证码",
          },
        ],
      },
    };
  },
};
</script>
